package org.reliablesource.prism.core.internal;

import java.util.Observable;
import java.util.Observer;

import org.reliablesource.prism.core.IPrismElement;
import org.reliablesource.prism.core.IPrismModel;


/**
 * 
 * Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 * 
 * Created on: Mar 11, 2006
 * 
 * @author Dennis Park <a
 *         href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 * @model
 * 
 * A Prism model is it's own parent.
 * 
 * @since 1.0
 */

final class PrismModel extends PrismNode implements IPrismModel, Observer {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4533496601327059566L;

	PrismModel() {
		super();
		setParent(this);
	}

	/* (non-Javadoc)
	 * @see org.reliablesource.prism.core.internal.PrismNode#addChild(org.reliablesource.prism.core.IPrismElement)
	 */
	@Override
	public synchronized boolean addChild(IPrismElement element) {
		boolean _added = super.addChild(element);
		if(_added)
			((PrismElement)element).addObserver(this);
		return _added;
	}

	/* (non-Javadoc)
	 * @see org.reliablesource.prism.core.internal.PrismNode#removeChild(org.reliablesource.prism.core.IPrismElement)
	 */
	@Override
	public synchronized boolean removeChild(IPrismElement element) {
		boolean _removed = 
			super.removeChild(element);
		
		if(_removed)
			((PrismElement)element).deleteObserver(this);
		return _removed;
	}

	public void update(Observable arg0, Object _updateObject) {
		// just forward to model's subscribers
		notifyObservers(_updateObject);
	}
}
